package cn.chencq.moudle.proxyip.service.impl;

import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.chencq.moudle.lottery.service.impl.LotteryServiceImpl;
import cn.chencq.moudle.proxyip.dao.ProxyIpDao;
import cn.chencq.moudle.proxyip.entity.ProxyIp;
import cn.chencq.moudle.proxyip.service.ProxyIpService;
import cn.chencq.moudle.sys.dao.BaseDao;
import cn.chencq.moudle.sys.service.impl.BaseServiceImpl;
import cn.chencq.utils.MD5Utils;

@Service
public class ProxyIpServiceImpl extends BaseServiceImpl<ProxyIp> implements ProxyIpService {

	private static final Logger logger = LoggerFactory.getLogger(LotteryServiceImpl.class);

	@Autowired
	private ProxyIpDao proxyIpDao;

	@Override
	protected BaseDao getBaseDao() {
		return proxyIpDao;
	}

	@Override
	public void save(ProxyIp t) {
		t.setId(getId(t));
		proxyIpDao.save(t);
	}

	@Override
	public void batchSaveOrUpdate(List<ProxyIp> list) {
		if (Objects.isNull(list) || list.isEmpty()) {
			return;
		}
		
		list.stream().forEach(t -> {
			t.setId(getId(t));
		});
		
		proxyIpDao.batchSaveOrUpdate(list);
	}

	private String getId(ProxyIp p) {
		String ming = p.getIp() + ":" + p.getPort();
		return MD5Utils.getMD5Code(ming);
	}

}
